//
//  MainViewController.swift
//  weibo
//
//  Created by huangtuo on 16/6/3.
//  Copyright © 2016年 huangtuo. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    
    //MARK:按钮监听方法
    @objc private func clickComposedButton(){
        print("点我了")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        
         setupComposedButton()
    }
    
    override func viewWillAppear(animated: Bool) {
        // 会创建 tabBar 中的所有控制器对应的按钮！
        super.viewWillAppear(animated)
        //让按钮在最前面展示
        tabBar.bringSubviewToFront(composedButton)
    }
    
    //MARK:懒加载控件
    lazy var composedButton: UIButton = UIButton(
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
    
}

//MARK:设置界面
extension MainViewController{
    //设置按钮
    private func setupComposedButton(){
        tabBar.addSubview(composedButton)
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count) - 1
        composedButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
    //添加所以控制器
    private func addChildViewControllers() {
        
        //提高性能,能用颜色就不要用图片
        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(HomeTableViewController(),title: "首页",imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    private func addChildViewController(vc: UIViewController,title:String,imageName:String) {
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
    
    
}
















